#include "LayerContainerBody.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "deprecated/CCDeprecated.h"
#include "ui/UIHelper.h"
#include "base/CCConsole.h"

#include "logic/LogicData.h"
#include "logic/LLogicContainer.h"

USING_NS_CC;
using namespace ui;

BodyIndexStatusInfo	CLayerContainerBody::m_BodyStatus;

CLayerContainerBody::CLayerContainerBody(void) : m_ContainerBody(CLogicData::GetInstance()->GetOneself()->GetContainerBody())
{
	for (int32_t i = 0; i != MAX_CONTAINER_BOX_INDEX_SIZE; ++i) {
		BodyItemData tData;
		m_ItemData.push_back(tData);
	}
}


CLayerContainerBody::~CLayerContainerBody(void)
{
}

bool CLayerContainerBody::init() {
	// 1. super init first
	if ( !CLayerBase::init() )
	{
		return false;
	}

	//m_Pack = CSLoader::createNode("Inventory.csb");

	//ui::Button* tLoginB = (ui::Button*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_Pack, 93);
	//tLoginB->addTouchEventListener(this, toucheventselector(CSceneLogin::onLogin));

	//addChild(m_Pack);

	return true;
}

void CLayerContainerBody::onEnter() {
	CLayerBase::onEnter();

	m_NodeBody = CSLoader::createNode("ContainerBody.csb");
	m_NodeBody->setContentSize(cocos2d::Size(450,500));
	cocos2d::ui::ListView* tList = (cocos2d::ui::ListView*)ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_NodeBody, 344);
	//tList->addEventListenerListView(this, listvieweventselector(CLayerContainerBody::onSelectedItemEvent));
	//tList->addEventListener((ListView::ccListViewCallback)CC_CALLBACK_2(CLayerContainerBody::onItem, this));
	tList->addTouchEventListener(this, toucheventselector(CLayerContainerBody::onItem));
	auto tButton = ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)m_NodeBody, 351);
	tButton->addTouchEventListener(this, toucheventselector(CLayerContainerBody::onClose));

	for (uint8_t j = 0; j != MAX_CONTAINER_BOX_INDEX_SIZE; ++j) {
		CGLGoodsBase* tGoods = m_ContainerBody->GetGoods(1, j);
		if (nullptr != tGoods) {
			BodyItemData* tData = &m_ItemData[j];
			tData->m_BodyInfo = tGoods->GetGoodsInfo();
			tData->m_index = j;

			auto tNode1 = CSLoader::createNode("ContainerBodyItem.csb");
			auto tListLayout = ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)tNode1, 24);

			tButton = ui::Helper::seekWidgetByTag((cocos2d::ui::Widget*)tListLayout, 352);
			tListLayout->addTouchEventListener(this, toucheventselector(CLayerContainerBody::onStatusBattle));
			tListLayout->setUserData(tData);
			
			tList->pushBackCustomItem(tListLayout);
		}
	}

	this->addChild(m_NodeBody);
}
/*
void CLayerContainerBody::onExit() {
	CLayerBase::onExit();
}

void CLayerContainerBody::update(float delta) {
	CLayerBase::update(delta);
}
*/

void CLayerContainerBody::SetBodyStatus(BodyStatusModel model, uint8_t index) {
	m_BodyStatus.m_StatusIndex[model] = index;
}

void CLayerContainerBody::onClose(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		this->setVisible(false);
		break;
	}
}

void CLayerContainerBody::onStatusBattle(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case cocos2d::ui::TOUCH_EVENT_ENDED: {
		cocos2d::ui::Widget* tButton = (cocos2d::ui::Widget*)(pSender);
		BodyItemData* tData = (BodyItemData*)tButton->getUserData();
		CCLOG("~~~~~~~~~~~~~~~TOUCH_EVENT_ENDED~~~~~~~~~~~~~~~~%d", tData->m_index);
		Index_t& tIndex = m_BodyStatus.m_StatusIndex[BSMBattle];
		if (tIndex == Invalid_Index) {
			CTLLogicContainer tLogicContainer;
			tLogicContainer.SetBattleBody(tData->m_index);

			tIndex = tData->m_index;
		} else {
			CTLLogicContainer tLogicContainer;
			tLogicContainer.SetBattleBody(Invalid_Index);

			tIndex = Invalid_Index;
		}
		} break;
	}
}

void CLayerContainerBody::onItem(cocos2d::Ref* pSender, cocos2d::ui::TouchEventType type) {
	switch (type)
	{
	case TOUCH_EVENT_BEGAN:
		{
			ListView* listView = static_cast<ListView*>(pSender);

			CCLOG("TOUCH_EVENT_BEGAN~~~~~~~~~~~~~~~~%ld",listView->getCurSelectedIndex());
		} break;
	case TOUCH_EVENT_MOVED:
		{
			ListView* listView = static_cast<ListView*>(pSender);

			CCLOG("TOUCH_EVENT_MOVED~~~~~~~~~~~~~~~~%ld",listView->getCurSelectedIndex());
		} break;
	case cocos2d::ui::TOUCH_EVENT_ENDED:
		{
			ListView* listView = static_cast<ListView*>(pSender);

			CCLOG("TOUCH_EVENT_ENDED~~~~~~~~~~~~~~~~%ld",listView->getCurSelectedIndex());
		}
		break;
	default:
		break;
	}
}

void CLayerContainerBody::onSelectedItemEvent(Ref *pSender, cocos2d::ui::ListViewEventType type) {
	switch (type) {
	case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_START:
		{
			ListView* listView = static_cast<ListView*>(pSender);

			CCLOG("LISTVIEW_ONSELECTEDITEM_START~~~~~~~~~~~~~~~~%ld",listView->getCurSelectedIndex());
		}
		break;


	case cocos2d::ui::LISTVIEW_ONSELECTEDITEM_END:
		{
			ListView* listView = static_cast<ListView*>(pSender);

			CCLOG("LISTVIEW_ONSELECTEDITEM_END~~~~~~~~~~~~~~~~~~~~~~~%ld",listView->getCurSelectedIndex());
		}
		break;
	default:
		break;
	}
}
